<?php

App::uses('Crud', 'Model');
App::uses('CertificationDetail', 'Model');
App::uses('Certification', 'Model');
App::uses('Line', 'Model');
App::uses('Workstation', 'Model');

class WorkstationConfig extends Crud {

    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const TYPE_EVALUACION = 1;
    const TYPE_VALIDACION = 2;
    const TYPE_CERTIFICACION = 3;
    public $useTable = 'workstations_configs';

    /**
     * Obtenemos las configuraciones por Operación
     * @param string $workstationId
     * @return array
     */
    public function getEnabledByWorkstation($workstationId) {
        $filters = array(
            'fields' => array(
                'id',
                'name',
                'minimum_qualification',
                'questions',
                '`time`/60 as time',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'workstation_id' => $workstationId,
            ),
            'order' => array(
                'orden' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getByWorkstationAndName($workstationId, $name) {
        $filters = array(
            'fields' => array(
                'id',
            ),
            'conditions' => array(
                'workstation_id' => $workstationId,
                'name' => $name,
            ),
            'order' => array(
                'orden' => 'ASC'
            ),
        );
        return $this->find('first', $filters);
    }

    /**
     * Obtenemos todas los temas de las Operaciones con los que se puede relacionar
     * un tema de la Operación.
     * @param string $workstationConfigId UUID
     * @return array
     */
    public function getPossibleDependencies($workstationConfigId) {
        $filters = array(
            'fields' => array(
                'CONCAT(l2.name, "-", w2.description, "-", wc2.name) as text',
                'wc2.id as value',
            ),
            'joins' => array(
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'WorkstationConfig.workstation_id = w.id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.id = w.line_id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l2',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l2.area_id = l.area_id',
                        'l2.status' => Line::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w2',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w2.line_id = l2.id',
                        'w2.status' => Workstation::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc2',
                    'type' => 'INNER',
                    'conditions' => array(
                        'wc2.workstation_id = w2.id',
                        'wc2.status' => self::STATUS_ENABLED,
                        'wc2.id !=' => $workstationConfigId
                    ),
                ),
            ),
            'conditions' => array(
                'WorkstationConfig.id' => $workstationConfigId,
            ),
            'order' => array(
                'l2.name ASC',
                'w2.name ASC',
                'wc2.orden ASC',
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    public function getBreadcrumb($id) {
        $filters = array(
            'fields' => array(
                'm.name as name',
                'a.name as name',
                'l.name as name',
                'w.description as name',
                'WorkstationConfig.name as name',
            ),
            'joins' => array(
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'inner',
                    'conditions' => array(
                        'w.id = WorkstationConfig.workstation_id'
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'inner',
                    'conditions' => array(
                        'l.id = w.line_id'
                    ),
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = l.area_id'
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id'
                    ),
                ),
            ),
            'conditions' => array(
                'WorkstationConfig.id' => $id,
            ),
        );
        return $this->find('first', $filters);
    }

//    public function isCertificationSuccess($certificationId, $workstationId)
//    {
//        $filters = array(
//            'fields' => array(
//                'count(WorkstationConfig.id) as c'
//            ),
//            'joins' => array(
//                array(
//                    'table' => 'certifications_details',
//                    'alias' => 'cd',
//                    'type' => 'LEFT',
//                    'conditions' => array(
//                        'cd.workstation_config_id = WorkstationConfig.id',
//                        'cd.certification_id' => $certificationId,
//                        'cd.status' => self::STATUS_ENABLED,
//                    ),
//                ),
//            ),
//            'conditions' => array(
//                'WorkstationConfig.workstation_id' => $workstationId,
//                'WorkstationConfig.status' => self::STATUS_ENABLED,
//                'OR' => array(
//                    'cd.qualification IS NULL',
//                    'AND' => array(
//                        'cd.qualification IS NOT NULL',
//                        'cd.qualification < WorkstationConfig.minimum_qualification',
//                    )
//                )
//            ),
//        );
//        $record = $this->find('first', $filters);
//        return $record[0]['c'] == 0;
//    }

    public function getForExam( $wsId )
    {
        return $this->getByNameAndWSId('Certificación en piso', $wsId);
    }

    private function getByNameAndWSId($name, $wsId)
    {
        $like = '%' . strtolower($name) . '%';
        $filters = array(
            'conditions' => array(
                'workstation_id' => $wsId,
                'LOWER( name ) LIKE' => $like
            )
        ); //Obtener el workstation_config
        return $this->find('first', $filters);
    }
    
    public function getEvaluationForExam( $wsId )
    {
        return $this->getByNameAndWSId('Evaluación en piso', $wsId);
    }
    
    public function getValidationForExam( $wsId )
    {
        return $this->getByNameAndWSId( 'Validación en piso', $wsId);
    }
}
